package cn.tedu.xxmall.controller;

import cn.tedu.xxmall.pojo.dto.AdminAddNewDTO;
import cn.tedu.xxmall.pojo.dto.AdminLoginDTO;
import cn.tedu.xxmall.pojo.entity.Admin;
import cn.tedu.xxmall.service.IAdminService;
import cn.tedu.xxmall.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "管理员模块")
@Slf4j
@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    IAdminService adminService;


    //http://localhost:8888/admin/add
    @ApiOperation("添加管理员")
    @PostMapping("/add")
    @PreAuthorize("hasAuthority('/ams/admin/update')")
    public JsonResult adminAdd(AdminAddNewDTO adminAddNewDTO){
        log.debug("开始添加管理员");
        adminService.addNew(adminAddNewDTO);
        return JsonResult.ok("添加管理员完成");
    }

    //http://localhost:8888/admin/login
    @PostMapping("/login")
    @ApiOperation("管理员登录")
    public JsonResult<String> login(AdminLoginDTO adminLoginDTO){
        log.debug("登录管理员账户");
        String jwt = adminService.login(adminLoginDTO);
        return JsonResult.ok(jwt);
    }
    @PostMapping("/delete")
    @ApiOperation("根据用户名删除管理员")
    public JsonResult delete(Admin admin){
        adminService.delete(admin);
        return JsonResult.ok("删除完成");
    }

}
